import { Item } from "@/api/types/base";
import { RenderScope } from "@/components/ProTable/interface";
import { useDateFormat } from "@vueuse/core";

export const timeRender: any = (scope: RenderScope<Item>) => {
  if (scope.row[scope.column.property]) {
    return useDateFormat(scope.row[scope.column.property], "YYYY-MM-DD HH:mm:ss").value;
  }
};

export const timeFormatter: any = (row, column, cellValue) => {
  if (cellValue) {
    return useDateFormat(cellValue, "YYYY-MM-DD HH:mm:ss").value;
  }
};

export const dateRender: any = (scope: RenderScope<Item>) => {
  if (scope.row[scope.column.property]) {
    return useDateFormat(scope.row[scope.column.property], "YYYY-MM-DD").value;
  }
};

export const yearRender: any = (scope: RenderScope<Item>) => {
  if (scope.row[scope.column.property]) {
    return useDateFormat(scope.row[scope.column.property], "YYYY").value;
  }
};

export const monthRender: any = (scope: RenderScope<Item>) => {
  if (scope.row[scope.column.property]) {
    return useDateFormat(scope.row[scope.column.property], "YYYY-MM").value;
  }
};

export const productRender: any = (scope: RenderScope<Item>, productList) => {
  let title;
  if (scope.row[scope.column.property] && productList.value.length > 0) {
    title = productList.value.find(item => item._id === scope.row[scope.column.property])?.name;
  }
  return title;
};

export const orgRender: any = (scope: RenderScope<Item>, orgList) => {
  let title;
  if (scope.row[scope.column.property]) {
    if (orgList.value && orgList.value.length > 0) {
      title = orgList.value.find(item => item._id === scope.row[scope.column.property])?.name;
    } else if (orgList && orgList.length) {
      title = orgList.find(item => item._id === scope.row[scope.column.property])?.name;
    }
  }
  return title;
};

export const enumTreeRender: any = (scope: RenderScope<Item>, treeDataList) => {
  let title;
  if (scope.row[scope.column.property] && treeDataList.value.length > 0) {
    title = treeDataList.value.find(item => item._id === scope.row[scope.column.property])?.name;
  }
  return title;
};

export const enumRender: any = (scope: RenderScope<Item>, dataList, idProName = "_id") => {
  let title;
  if (dataList.value && scope.row[scope.column.property]) {
    title = dataList.value.find(item => item[idProName] === scope.row[scope.column.property])?.name;
  }
  return title;
};

export const userRender: any = (scope: RenderScope<Item>, dataList) => {
  let title;
  if (scope.row[scope.column.property] && dataList.value) {
    title = dataList.value.find(item => item._id === scope.row[scope.column.property])?.userName;
  }
  return title;
};

export const enName2CnNameRender: any = (scope: RenderScope<Item>, dataList) => {
  let title;
  if (scope.row[scope.column.property] && dataList.value) {
    title = dataList.value.find(item => item.enName === scope.row[scope.column.property])?.cnName;
  }
  return title;
};

export const refRender: any = (scope: RenderScope<Item>, dataList, columnName, id = "_id", name = "name") => {
  let title;
  if (scope.row[columnName] && dataList.value) {
    title = dataList.value.find(item => item[id] === scope.row[columnName])?.[name];
  }
  return title;
};
